package com.sline.TCP;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Demo2_Server {
    public static void main(String[] args) throws IOException {
//        ServerSocket server = new ServerSocket(12345);
//        Socket socket = server.accept();
//        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//        PrintStream ps = new PrintStream(socket.getOutputStream());
//
//        while (true) {
//            String msg = br.readLine();
//
//            System.out.println("服务器收到请求内容: " + msg);
//
//            ps.println("百度一下, 你就知道~");
//        }
        ServerSocket server = new ServerSocket(12345);


        while (true) {

            Socket socket = server.accept();

            new Thread() {
                public void run() {
                    while (true) {
                        try {
                            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                            PrintStream ps = new PrintStream(socket.getOutputStream());

                            String msg = br.readLine();

                            System.out.println("服务器收到请求内容: " + msg);

                            ps.println("百度一下, 你就知道~");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
        }
    }
}
